package interfazeak;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import db.BarietateaKud;
import db.UpelaKud;

public class GehituBarietatea extends JDialog {
	private BarietateaKonf guraso;
	private final JPanel contentPanel = new JPanel();
	private JLabel barietatealbl;
	private JTextField barietateatxt;
	private JButton gordebtn;

	public static void main(String[] args) {
		try {
			final Boolean MODALA = true;
			GehituBarietatea dialog = new GehituBarietatea(new BarietateaKonf(null, MODALA), MODALA);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public GehituBarietatea(BarietateaKonf guraso, Boolean mODALA) {
		
		this.guraso = guraso;

		setBounds(100, 100, 300, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		setTitle("GEHITU BARIETATEA");
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		

		barietatealbl = new JLabel("BARIETATEA");
		barietatealbl.setBounds(90, 80, 120, 25);
		contentPanel.add(barietatealbl);
		
		barietateatxt = new JTextField();
		barietateatxt.setBounds(70, 120, 120, 25);
		contentPanel.add(barietateatxt);
		
		gordebtn = new JButton("GORDE");
		gordebtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				String barietatea = barietateatxt.getText();
				if (barietatea.equals("")){
					JOptionPane.showMessageDialog(GehituBarietatea.this,
							"Sartu barietatea!", "KONTUZ",
							JOptionPane.WARNING_MESSAGE);
				}
				else {
					
					boolean badago = GehituBarietatea.this.badagoBarietatea(barietatea);
					if (badago == true) {
						JOptionPane.showMessageDialog(GehituBarietatea.this,
								"Barietate hau gordeta dago!", "KONTUZ",
								JOptionPane.WARNING_MESSAGE);
					} else {
						int konfirmatu = JOptionPane.showConfirmDialog(
								GehituBarietatea.this, "Gorde nahi duzu?");
						
						if (JOptionPane.OK_OPTION==konfirmatu){
					GehituBarietatea.this.gorde(barietatea);
					GehituBarietatea.this.dispose();
					GehituBarietatea.this.guraso.ezabatutaula();
					GehituBarietatea.this.guraso.bistaratu();
						}
						else if (JOptionPane.NO_OPTION==konfirmatu){
							GehituBarietatea.this.dispose();

						}
				}
				}
			}
		});
		gordebtn.setBounds(70, 210, 120, 25);
		contentPanel.add(gordebtn);
	}
	protected boolean badagoBarietatea(String barietatea) {
		int emaitza = BarietateaKud.badagoBarietatea(barietatea);
		if (emaitza == 0) {
			return false;
		} else {
			return true;
		}
	}
	protected void gorde(String barietatea) {
	
		BarietateaKud.gehituBarietatea(barietatea);
	}
}
